//index.js
//获取应用实例
var app = getApp();
const { Post } = require('../../../utils/wxb.js');
var wxb = require('../../../utils/wxb.js');
Page({
  data: {
    color: '',
    gps_addr: '',
    lng: '',
    lat: '',
    address_id: 0,
    datas: [],
  },



  map: function () {
    wx.chooseLocation({
      success: function (data) {
        wxb.that.setData({
          gps_addr: data.address,
          lng: data.longitude,
          lat: data.latitude
        });
      }
    });
  },

  /**
   * 获取地址详情
   */
  getAddressDetail: function (e) {
    wx.showLoading({
      title: '加载中...',
    })

    wxb.Post('/api/user/addressDetail', {
      openid: wxb.getOpenId(),
      address_id: wxb.that.data.address_id,
    }, function (data) {
      wx.hideLoading();
      wxb.that.setData({
        datas: data,
        gps_addr: data.gps_addr,
        lng: data.lng,
        lat: data.lat,
      });
    });
  },

  fromSubmit: function (data) {
    var data = data.detail.value;
    if (!data.name) {
      wx.showToast({
        image: '../../../img/cw.png',
        title: '联系人不能为空',
      })
    } else if (!data.mobile) {
      wx.showToast({
        image: '/img/kulian.png',
        title: '手机号码不能为空',
      })
    } else if (!data.gps_addr) {
      wx.showToast({
        image: '/img/kulian.png',
        title: '请选择GPS定位',
      })
    } else if (!data.lng || !data.lat) {
      wx.showToast({
        image: '/img/kulian.png',
        title: '请选择GPS定位',
      })
    } else if (!data.address) {
      wx.showToast({
        image: '/img/kulian.png',
        title: '请选择GPS定位',
      })
    } else {
     
     wx.request({
       url:app.globalData.api+'addAddress',
      method:"Post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        name:data.name,
        phone:data.mobile,
        gpsAddr:data.gps_addr,
        describe:data.address,
        openid:wx.getStorageSync('openid')

      },
      success:function(){
        wx.showModal({
          title: '提示',
          content: '添加成功',
          showCancel:false,
          success(){
            wx.navigateBack({
              delta: 0,
            })
          }
        })
      }
     })
    

    }
  },

  onLoad: function (e) {
    wxb.that = this;
    wxb.style();
  }
})
